package com.wap;

import java.io.File;
import java.util.ArrayList;

import com.base.Constant;
import com.base.DatabaseObject;

import com.base.DatabaseDAO;
import com.utils.Md5;

public class User extends DatabaseDAO {
	private Long userId;
	private String userName;
	private String password;
	private String fullName;
	private String email;
	private String mobile;
	private String address;
	private String status;
	private String brandId;

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public boolean registeUser() {
		boolean isok = false;
		isok = insertChannel(this.userName);
		if (isok)
			isok = insertUser(this.userName, this.password, this.email,
					this.mobile, this.address);
		return isok;
	}

	public void createUserFolder(String currentPath) {

		String pathfile = currentPath + "\\" + this.userName + "\\";
		if (!(new File(pathfile)).exists())
			(new File(pathfile)).mkdirs();
	}

	public void changePassword() {

	}
	public boolean logIn() {
		return logIn(this.userName, this.password);
	}

	public boolean validateData() {

		if ((this.userName == null) || (this.password == null))
			return false;
		if (this.email == null) {
			return false;
		} else { // validate format email

		}
		if (this.mobile == null)
			return false;
		else {
			// validate phone number
		}
		return true;
	}

	public boolean checkExist() {
		if (checkChannelCodeExist(this.userName))
			if (checkUserNameExist(this.userName))
				return true;
			else
				return false;
		else
			return false;
	}

	private boolean checkUserNameExist(String userName) {

		if ((this.userName == null))
			return false;
		else {
			ArrayList<DatabaseObject> result = findByDynamicWhere(
					Constant.DB_POOL_MWAP, "adm_user", new String[] {
							"user_id", "user_name", "password", "status",
							"mobile", "address", "branch_id" }, new String[] {
							null, null, null, null, null, null, null },
					"user_name=?", new Object[] { userName }, null);
			if (result != null && !result.isEmpty())
				return false;
		}
		return true;
	}

	private boolean checkChannelCodeExist(String userName) {

		if ((this.userName == null))
			return false;
		else {
			ArrayList<DatabaseObject> result = findByDynamicWhere(
					Constant.DB_POOL_MWAP, "channel", new String[] {
							"channel_id", "code", "name", "current_version",
							"jad_url", "jar_url", "download_count" },
					new String[] { null, null, null, null, null, null, null },
					"code=?", new Object[] { userName }, null);
			if (result != null && !result.isEmpty())
				return false;
		}
		return true;
	}

	private boolean insertChannel(String userName) {
		DatabaseDAO databaseDAO = new DatabaseDAO();
		String[] fields = { "code", "name", "jad_url", "jar_url" };
		String[] values = { userName, userName, null, null };
		return databaseDAO.insertRowWithSEQ(Constant.DB_POOL_MWAP, "Channel",
				"channel_id", "seq_channel.nextval", fields, values);
	}

	private boolean insertUser(String userName, String password, String email,
			String mobile, String address) {
		DatabaseDAO databaseDAO = new DatabaseDAO();
		String[] fields = { "user_name", "password", "email", "mobile",
				"address" };
		String[] values = { userName, password, email, mobile, address };
		return databaseDAO.insertRowWithSEQ(Constant.DB_POOL_MWAP, "adm_user",
				"user_id", "adm_user_seq.nextval", fields, values);
	}

	private boolean logIn(String userName, String password) {
		ArrayList<DatabaseObject> users = null;
		DatabaseDAO databaseDAO = new DatabaseDAO();
		String[] fields = { "user_name", "password", "email", "mobile",
				"address" };
		String[] alias = { "user_name", "password", "email", "mobile",
				"address" };
		String where = " user_name =? and password = ?";
		String[] whereValues = { userName, password };
		users = databaseDAO.findByDynamicWhere(Constant.DB_POOL_MWAP,
				"adm_user", fields, alias, where, whereValues, null);
		if (users != null && !users.isEmpty())
			return true;
		return false;
	}
}
